package com.zkingsoft.web.tag;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.matrix.core.tools.StringUtils;
import com.zkingsoft.web.bean.ProductAttrDef;
import com.zkingsoft.web.bean.ProductAttrRef;
import com.zkingsoft.web.dao.ProductAttrDefDao;
import com.zkingsoft.web.dao.ProductAttrRefDao;
import com.zkingsoft.web.dao.ProductDao;
import com.zkingsoft.web.tool.SystemConstance;
import com.zkingsoft.web.tool.WebUtil;

import net.sf.json.JSONArray;

public class ProductTag extends SimpleTagSupport {

	/**
	 * 产品id
	 */
	private String id;

	/**
	 * 变量名称
	 */
	private String var;

	/**
	 * 是否需要查询sku信息
	 */
	private boolean needSku = false;

	/**
	 * 获取产品
	 * 
	 * @author jiangyouyao
	 */
	public void doTag() throws JspException, IOException {
		ProductDao productDao = WebUtil.getService(this.getJspContext(), SystemConstance.PRODUCTDAO);
		ProductAttrDefDao productAttrDefDao = WebUtil.getService(this.getJspContext(),
				SystemConstance.PRODUCT_ATTRDEFDAO);
		ProductAttrRefDao productAttrRefDao = WebUtil.getService(this.getJspContext(),
				SystemConstance.PRODUCT_ATTRREFDAO);

		if (needSku) {
			// 查询sku去重排列
			ProductAttrRef productAttrRef = new ProductAttrRef();
			productAttrRef.setProductId(id);
			productAttrRef.setIsSku(ProductAttrRef.SKU);
			List<ProductAttrRef> refList = productAttrRefDao.selectByModel(productAttrRef);
			//吧这些sku转为json字符串存在界面
			 JSONArray skusImgsJson = JSONArray.fromObject(refList);
			 this.getJspContext().setAttribute("skusImgsJson", skusImgsJson.toString());
			
			// 记录去重后的skuId
			List<String> skuIds = new ArrayList<>();
			for (ProductAttrRef proAttrRef : refList) {
				String attrs = proAttrRef.getAttrId();
				if (StringUtils.isNotBlank(attrs)) {
					List<String> attrList = StringUtils.strToCollToString(attrs, ",");
					for (String attrId : attrList) {
						if (!"-".equals(attrId) && !skuIds.contains(attrId)) {
							skuIds.add(attrId);
						}
					}
				}
			}

			// 获取到sku定义信息
			List<ProductAttrDef> productAttrDefList = productAttrDefDao.selectByIds(skuIds);
			
			
			Map<String, ProductAttrDef> paentAttrMap = new HashMap<>();
			
			for (ProductAttrDef productAttrDef : productAttrDefList) {
				String parentId = productAttrDef.getParentId();
				if (!paentAttrMap.containsKey(parentId)) {
					ProductAttrDef parent = productAttrDefDao.selectById(parentId);
					List<ProductAttrDef> childs = new ArrayList<>();
					childs.add(productAttrDef);
					parent.setChilds(childs);
					paentAttrMap.put(parentId, parent);
				} else {
					ProductAttrDef parent = paentAttrMap.get(parentId);
					parent.getChilds().add(productAttrDef);
				}
			}
			AttrDefComparator attrDefComparator=new AttrDefComparator();
			// 处理排序
			Set<String> keys = paentAttrMap.keySet();
			List<ProductAttrDef> result = new ArrayList<>();
			for (String key : keys) {
				Collections.sort(paentAttrMap.get(key).getChilds(),attrDefComparator);
				result.add(paentAttrMap.get(key));
			}
			Collections.sort(result, attrDefComparator);
			this.getJspContext().setAttribute("skuList", result);
		}

		this.getJspContext().setAttribute(var, productDao.selectById(id));
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	

	public boolean isNeedSku() {
		return needSku;
	}

	public void setNeedSku(boolean needSku) {
		this.needSku = needSku;
	}



	StringWriter sw = new StringWriter();

}